package com.xhwl.centiotdata.handler.pedestrian.impl;

import com.xhwl.centiotdata.handler.pedestrian.PedestrianIntegrationFactory;
import com.xhwl.centiotdata.handler.pedestrian.PedestrianIntegrationHandler;
import com.xhwl.centiotdata.pojo.constant.FuShiConstant;
import com.xhwl.centiotdata.pojo.dto.dahua.PedestrianRecordDaHuaDTO;
import com.xhwl.centiotdata.pojo.dto.fushi.PedestrianRecordFuShiDTO;
import com.xhwl.centiotdata.pojo.dto.pedestrian.PedestrianIntegrationConfigDTO;
import com.xhwl.centiotdata.service.cent.IAuthAccountCentService;
import com.xhwl.centiotdata.service.cent.IPedestrianCentService;
import com.xhwl.centiotdata.suppliersystem.fushi.PedestrianFuShiHandler;
import com.xhwl.centiotdata.utils.RedisUtils;
import com.xhwl.common.dto.auth.PedestrianAuthDTO;
import com.xhwl.common.dto.cent.pedes.OpenDoorRecordDTO;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.auth.AuthAccountMapping;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Author: yanyixiong
 * @Description: 人行 富士对接
 * @Date: create in 13:36 2023/4/21
 */
@Service
public class PedestrianIntegrationFuShiHandler extends PedestrianIntegrationHandler {

    @Autowired
    private IAuthAccountCentService authAccountCentService;
    @Autowired
    private PedestrianFuShiHandler pedestrianFuShiHandler;
    @Autowired
    private IPedestrianCentService pedestrianCentService;
    @Autowired
    private RedisUtils redisUtils;

    @Override
    public void afterPropertiesSet() throws Exception {
        PedestrianIntegrationFactory.register(96, this);
    }

    @Override
    public void pullRecord(PedestrianIntegrationConfigDTO configDTO, Integer intervalDays, Date date) {
        // 拉取数据
        List<PedestrianRecordFuShiDTO> recordFuShiDTOList = pedestrianFuShiHandler.pullRecord(configDTO,intervalDays,date);
        if (CollectionUtils.isEmpty(recordFuShiDTOList)) {
            return;
        }
        // 转换数据
        List<OpenDoorRecordDTO> openDoorRecordDTOList = pedestrianFuShiHandler.handleRecord(configDTO, recordFuShiDTOList);
        // 存储业务中台
        pedestrianCentService.saveBatch(openDoorRecordDTOList);
    }



    @Override
    public ResultJson createAuth(PedestrianIntegrationConfigDTO configDTO, PedestrianAuthDTO pedestrianAuthDTO) {
        AuthAccountMapping accountMapping = pedestrianFuShiHandler.createUser(configDTO, pedestrianAuthDTO);
        String openId = accountMapping.getOpenId();
        pedestrianAuthDTO.setUserOpenId(openId);
        //添加人脸并授权
        String faceUrl = pedestrianAuthDTO.getFaceUrl();
        if (StringUtils.isNotBlank(faceUrl)){
            pedestrianFuShiHandler.addFace(openId, faceUrl, configDTO);
            String key = String.format(FuShiConstant.USER_FACE_KEY, accountMapping.getProjectId(), accountMapping.getAccountId());
            redisUtils.set(key, faceUrl);
            pedestrianFuShiHandler.saveAuth(configDTO, pedestrianAuthDTO, 2);
        }
        //卡授权
        if(CollectionUtils.isNotEmpty((pedestrianAuthDTO.getCardNumberList()))
                || StringUtils.isNotBlank(pedestrianAuthDTO.getCardNumber())){
            pedestrianFuShiHandler.saveAuth(configDTO, pedestrianAuthDTO, 1);
        }


        return ResultJson.success();
    }



    @Override
    public ResultJson updateAuth(PedestrianIntegrationConfigDTO configDTO, PedestrianAuthDTO pedestrianAuthDTO) {
        AuthAccountMapping accountMapping = authAccountCentService.query(configDTO.getProjectId(), pedestrianAuthDTO.getSourceId(), configDTO.getProductId());
        String faceUrl = pedestrianAuthDTO.getFaceUrl();
        if (StringUtils.isNotBlank(faceUrl)){
            String key = String.format(FuShiConstant.USER_FACE_KEY,pedestrianAuthDTO.getProjectId(), pedestrianAuthDTO.getSourceId());
            String oldFaceUrl = (String) redisUtils.get(key);
            //人脸有更改
            if(!Objects.equals(oldFaceUrl, faceUrl)){
                pedestrianFuShiHandler.updateFace(accountMapping.getOpenId(), faceUrl, configDTO);
                pedestrianFuShiHandler.notify(accountMapping.getOpenId(), configDTO);
            }
        }
        //删除了人脸
        if(StringUtils.isBlank(faceUrl)){
            pedestrianFuShiHandler.deleteFace(accountMapping.getOpenId(), configDTO);
            pedestrianFuShiHandler.notify(accountMapping.getOpenId(), configDTO);
        }
        //新增或者修改卡授权
        if(CollectionUtils.isNotEmpty((pedestrianAuthDTO.getCardNumberList()))
                || StringUtils.isNotBlank(pedestrianAuthDTO.getCardNumber())){
            pedestrianFuShiHandler.updateAuth(configDTO, pedestrianAuthDTO, 1);
        }
        //新增或者修改人脸授权
        if(StringUtils.isNotBlank(faceUrl)){
            pedestrianFuShiHandler.updateAuth(configDTO, pedestrianAuthDTO, 2);
        }
        return ResultJson.success();
    }

    @Override
    public ResultJson deleteAuth(PedestrianIntegrationConfigDTO configDTO, PedestrianAuthDTO pedestrianAuthDTO) {

        pedestrianFuShiHandler.deleteAuth(configDTO, pedestrianAuthDTO);
        return ResultJson.success();
    }



}
